//
//  Log_y.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

//MARK: 日志打印库
public struct Log_y {
    
    // 日志等级
    public enum LogLevel: CustomStringConvertible {
        
        case verbose
        case debug
        case print
        case warning
        case error
        
        public var description: String {
            switch self {
            case .verbose:
                return " => 🟪 [VERBOSE]"
            case .debug:
                return " => 🟩 [DEBUG]"
            case .print:
                return " => 🟦 [PRINT]"
            case .warning:
                return " => 🟨 [WARNING]"
            case .error:
                return " => 🟥 [ERROR]"
            }
        }
    }
    
    public static func verbose<T>(_ message: T, file: String = #file, function: String = #function, line: UInt = #line) {
        log(message, at: .verbose, file: file, function: function, line: line)
    }
    
    public static func debug<T>(_ message: T, file: String = #file, function: String = #function, line: UInt = #line) {
        log(message, at: .debug, file: file, function: function, line: line)
    }
    
    public static func print_<T>(_ message: T, file: String = #file, function: String = #function, line: UInt = #line) {
        log(message, at: .print, file: file, function: function, line: line)
    }
    
    public static func warning<T>(_ message: T, file: String = #file, function: String = #function, line: UInt = #line) {
        log(message, at: .warning, file: file, function: function, line: line)
    }
    
    public static func error<T>(_ message: T, file: String = #file, function: String = #function, line: UInt = #line) {
        log(message, at: .error, file: file, function: function, line: line)
    }
    
    // 日志打印
    private static func log<T>(_ message: T,
                            at level: LogLevel,
                            file: String,
                            function: String,
                            line: UInt) {
#if DEBUG
        let fileName = (file as NSString).lastPathComponent
        let dateStr = Tools.stampToDateStr(Tools.stamp, type: ._YMDHMS)
        print("\(level.description) \(dateStr) \(fileName):\(line) \(function) || \(message)")
#endif
    }
}
